package com.ahdy.service.impl;

import com.ahdy.entity.MedicationPlan;
import com.ahdy.mapper.MedicationPlanMapper;
import com.ahdy.service.MedicationPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 用药计划服务实现类
 */
@Service
public class MedicationPlanServiceImpl implements MedicationPlanService {

    @Autowired
    private MedicationPlanMapper medicationPlanMapper;

    @Override
    @Transactional
    public Integer addMedicationPlan(MedicationPlan plan) {
        medicationPlanMapper.insert(plan);
        return plan.getId();
    }

    @Override
    @Transactional
    public int updateMedicationPlan(MedicationPlan plan) {
        return medicationPlanMapper.update(plan);
    }

    @Override
    @Transactional
    public int deleteMedicationPlan(Integer id) {
        return medicationPlanMapper.deleteById(id);
    }

    @Override
    public MedicationPlan getMedicationPlanById(Integer id) {
        return medicationPlanMapper.findById(id);
    }

    @Override
    public List<MedicationPlan> getMedicationPlansByUserId(Integer userId) {
        return medicationPlanMapper.findByUserId(userId);
    }

    @Override
    public List<MedicationPlan> getMedicationPlansByUserIdAndTimingType(Integer userId,
            MedicationPlan.TimingType timingType) {
        return medicationPlanMapper.findByUserIdAndTimingType(userId, timingType);
    }

    @Override
    @Transactional
    public int deleteMedicationPlansByUserId(Integer userId) {
        return medicationPlanMapper.deleteByUserId(userId);
    }

    @Override
    public List<Map<String, Object>> getMedicationPlanDetailsWithMedication(Integer userId) {
        return medicationPlanMapper.findPlanDetailsWithMedication(userId);
    }

    @Override
    public List<MedicationPlan> findConflictingPlans(Integer userId, MedicationPlan.TimingType timingType,
            String specificTime) {
        return medicationPlanMapper.findConflictingPlans(userId, timingType, specificTime);
    }
}